<template>
  <!-- 项目新增/编辑表单 -->
  <el-form ref="productFormRef" :model="formData" :rules="rules" label-width="80px">
    <el-form-item label="编码" prop="code">
      <el-input v-model="formData.code" placeholder="请输入编码" />
    </el-form-item>
    <el-form-item label="名称" prop="name">
      <el-input v-model="formData.name" placeholder="请输入名称" />
    </el-form-item>
    <el-form-item label="包名" prop="packageName">
      <el-input v-model="formData.packageName" placeholder="请输入包名" />
    </el-form-item>

    <el-form-item label="数据库" prop="dbType">
      <el-select v-model="formData.dbType" placeholder="请选择数据库" clearable>
        <el-option key="mysql" label="mysql" value="mysql" />
        <el-option key="sqlserver" label="sqlserver" value="sqlserver" />
      </el-select>
    </el-form-item>

    <el-form-item label="ip" prop="dbIp">
      <el-input v-model="formData.dbIp" style="max-width: 800px" placeholder="数据ip" />
    </el-form-item>
    <el-form-item label="端口" prop="dbPort">
      <el-input v-model="formData.dbPort" placeholder="数据库端口" />
    </el-form-item>

    <el-form-item label="项目名" prop="dbUserName">
      <el-input v-model="formData.dbUserName" placeholder="请输入项目名" maxlength="50" />
    </el-form-item>
    <el-form-item label="密码" prop="dbPassword">
      <el-input type="password" v-model="formData.dbPassword" placeholder="请输入密码" maxlength="50" show-password />
    </el-form-item>
  </el-form>
</template>
<script setup>
  import { findProductById, saveProduct } from '@/api/system/product/index';

  const props = defineProps({ id: { type: String, required: true } });

  let defaultFromData = {
    id: '',
    name: '',
    deptId: '',
    sex: '',
    mobile: '',
    email: '',
    loginName: '',
    status: 'enable',
  };

  const formData = reactive(Object.assign({}, defaultFromData));

  watch(
    () => props.id,
    () => {
      if (props.id) {
        loadData();
      } else {
        Object.assign(formData, defaultFromData, {});
      }
    },
    { immediate: true }
  );

  function loadData() {
    findProductById(props.id).then(({ data }) => {
      Object.assign(formData, defaultFromData, data);
    });
  }

  // 校验规则
  const rules = reactive({
    name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
    deptId: [{ required: true, message: '所属部门不能为空', trigger: 'blur' }],
    email: [
      {
        pattern: /\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/,
        message: '请输入正确的邮箱地址',
        trigger: 'blur',
      },
    ],
    mobile: [
      {
        pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
        message: '请输入正确的手机号码',
        trigger: 'blur',
      },
    ],
  });

  const productFormRef = ref(ElForm);

  const save = function () {
    productFormRef.value.validate((valid) => {
      if (valid) {
        saveProduct(formData, true).then(({ data }) => {
          formData.id = data.id;
          ElMessage.success('修改项目成功');
        });
      }
    });
  };

  const setValue = function (data) {
    Object.assign(formData, defaultFromData, data);
  };

  defineExpose({ save, setValue });
</script>
